﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DotNetX;
using System.Globalization;

namespace MyMainSite.Models
{
    public class BlogIndexModel : BaseModel
    {
        #region Properties

        #endregion

        #region Public Methods

        public BlogIndexModel(string blogFriendlyUrl, string title)
        {
            RichardsSiteEntities dc = new RichardsSiteEntities();

            this.CurrentBlog = (from b in dc.Blogs
                                where b.FriendlyUrl.Trim().ToLower() == blogFriendlyUrl.Trim().ToLower()
                                select b).SingleOrDefault();

            if (this.CurrentBlog == null)
                throw new InvalidOperationException(String.Format("'{0}' is an invalid blog friendly url.",
                    blogFriendlyUrl));

            this.BreadCrumb.Add(new KeyValuePair<string, string>(this.CurrentBlog.Name, this.CurrentBlog.FriendlyUrl));

            this.Create(title);
        }

        public BlogIndexModel(int year, int month)
            : base(year, month)
        {
            RichardsSiteEntities dc = new RichardsSiteEntities();

            this.CurrentBlog = null;

            this.BreadCrumb.Add(new KeyValuePair<string, string>(year.ToString(), year.ToString()));

            if (month > 0)
            {
                this.BreadCrumb.Add(new KeyValuePair<string, string>(CultureInfo.CurrentCulture.DateTimeFormat.GetAbbreviatedMonthName(month), month.ToString()));
            }
        }

        #endregion

        #region Private Methods

        private void Create(string title)
        {
            if (!String.IsNullOrEmpty(title))
            {
                this.CurrentFriendlyUrl = title;
                this.BreadCrumb.Add(new KeyValuePair<string, string>(title, title));
            }
        }

        #endregion
    }
}